package resource

import (
	"mallgo/internal/common/response"
	"mallgo/internal/model/waf"
	"mallgo/internal/resources/waf/api/domain/request"

	"gorm.io/gorm"
)

type WafResource struct {
	db          *gorm.DB
	ruleManager *waf.RuleManager
}

func NewWafResource(db *gorm.DB, ruleManager *waf.RuleManager) *WafResource {
	return &WafResource{
		db:          db,
		ruleManager: ruleManager,
	}
}

func (r *WafResource) CreateRule(req *request.RuleRequest) *response.ResponseObject {
	rule := waf.Rule{
		Type:        waf.RuleType(req.Type),
		Pattern:     req.Pattern,
		Description: req.Description,
		Status:      req.Status,
	}

	if err := r.ruleManager.AddRule(rule); err != nil {
		return response.Error(500, "创建规则失败")
	}

	return response.Success(rule)
}

func (r *WafResource) GetRules(ruleType string) *response.ResponseObject {
	rules := r.ruleManager.GetRules(waf.RuleType(ruleType))
	return response.Success(rules)
}
